﻿using CubicFWNet.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CubicFWNet.Data.SystemManager
{
    public class RoleRepository : RepositoryBase<Role>
    {

        /// <summary>
        /// 加载用户的所有角色
        /// </summary>
        public IEnumerable<Guid> LoadByUser(Guid userId)
        {
            var result = from userrole in dbcontext.Relevances
                         where userrole.FirstId == userId && userrole.Type == "UserRole"
                         select userrole.SecondId;
            return result;
        }

        /// <summary>
        /// 加载 角色的所有模块
        /// </summary>
        public IQueryable<Guid> LoadByRoleId(Guid roleId)
        {
            var result = from rolemodule in dbcontext.Relevances where rolemodule.FirstId == roleId && rolemodule.Type == "RoleModule"
                         select rolemodule.SecondId;
            return result;
        }


        /// <summary>
        /// 加载用户的所有机构
        /// </summary>
        public IEnumerable<Role> Find(int pageindex, int pagesize)
        {
            return dbcontext.Roles.OrderBy(u => u.SortNo).Skip((pageindex - 1) * pagesize).Take(pagesize);
        }

    }
}
